package Client.Model;

import java.io.IOException;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;

import Server.Player;

import rmi.ActiveGame;
import rmi.RMI_NavalBattle_Interface;
import rmi.RMI_Observer_Interface;
import rmi.UserInfo;


public class RMIClient extends UnicastRemoteObject implements RMI_Observer_Interface, Serializable{


	private static final long serialVersionUID = 2919111858343610950L;

	
	private static final String SERVICE_NAME = "rmiNavalBattle";
	
	private ArrayList<ActiveGame> games = new ArrayList<ActiveGame>();
	private ArrayList<UserInfo> users = new ArrayList<UserInfo>();
	
	RMI_NavalBattle_Interface service;
	

	public static void main(String args[])
	{
		String registry="localhost";

		if(args.length>=1){
			registry=args[0];}

		String registration= "rmi://"+registry+"/" + SERVICE_NAME;

		RMI_NavalBattle_Interface rGi;
		RMIClient cli;
		
		try {

			Remote rmiNavalBattle=Naming.lookup(registration);

			rGi=(RMI_NavalBattle_Interface)rmiNavalBattle;

			//System.out.println(((RMI_NavalBattle_Interface) rmiNavalBattle).activeGames());

			cli = new RMIClient(rGi);

			rGi.newObserver(cli);


		} catch (MalformedURLException | RemoteException | NotBoundException e) {
			e.printStackTrace();
			return;
		}
		
		pause();
		
		try {
			rGi.removeObserver(cli);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		
	}
	
	
	protected RMIClient(RMI_NavalBattle_Interface rGi) throws RemoteException {
		this.service = rGi;
	}

	
	@Override
	public void onDataUpdate() throws RemoteException {
	
		System.out.println("");
		
		users = service.getUsers();
		games = service.getGames();
		
		printUsers();
		
		printGames();
			
	}
	
	@Override
	public void onServerDown() throws RemoteException {
		
		System.out.println("Server down!");
		System.exit(1);
		
	}
	
	public void printUsers(){
		
		String state="?";
		
		System.out.println("--- Users ---");
		
		for (UserInfo u : users){
			
			if (u.getState()==Player.STATE_CONNECTED)
				state = "Connected";
			else
				if (u.getState()==Player.STATE_WAITING)
					state = "Waiting";
				else
					if (u.getState()==Player.STATE_PLAYING)
						state = "Playing";
			
			System.out.println("User: " + u.getName() + " [" + state + "]");
		}
			
		System.out.println("");
		
	}
	
	public void printGames(){
		
		
		System.out.println("--- Games ---");
		
		for (ActiveGame g : games)
			if (g.isActive())
				System.out.println("Game: " + g.getNamePlayer1() + " vs " + g.getNamePlayer1() + " (a decorrer)");
			else
				System.out.println("Game: " + g.getNamePlayer1() + " vs " + g.getNamePlayer1() + " (configurando)");
		
		System.out.println("");
		
	}

	
	static void pause() 
	{
		try
		{
			System.out.println("Press ENTER to exit..");
			System.in.read();
		}
		catch(IOException exe)
		{}
	}
	
}
